<?php

namespace app\common\lib;
use think\Exception;
class FileClass
{
    /**
     * 特定文件格式内容读取
     * @param $filePath
     * @return array|void
     */
    public static function specificFormatRead($filePath){
        try{
            $fn = fopen($filePath,"r");
            $work = [];
            while(!feof($fn))
            {
                $result = fgets($fn);
                # 去除换行符
                $result = str_replace(PHP_EOL, '', $result);
                $arrLine = explode("#", $result);
                array_push($work,$arrLine[0]);
            }
            fclose($fn);
            return $work;
        }catch (\Exception $e){
            logWrite('specificFormatRead', $e->getMessage(), __FILE__ );
            throw new \Think\Exception(config("config.describe.error"));
        }
    }


    /**
     * 文件内容读取
     * @param $filePath
     */
    public static function fileRead($filePath){
        try{
            $fn = fopen($filePath,"r");
            $work = [];
            while(!feof($fn))
            {
                $result = fgets($fn);
                # 去除换行符
                $result = str_replace(PHP_EOL,'',$result);
                array_push($work,$result);
            }
            fclose($fn);
            return $work;
        }catch (\Exception $e){
            logWrite('fileRead', $e->getMessage(), __FILE__ );
            throw new \Think\Exception(config("config.describe.error"));
        }
    }

    /**
     * 追加文件内容
     * @param $filePath // 文本文件路径
     * @param $array    // 追加的文件内容数组
     * @return mixed
     */
    public static function fileContentAdd($filePath,$array){
        try{
            $file = fopen($filePath,'ab+');
            if($array){
                foreach ($array as $key => $value){
                    if($value){
                        fwrite($file,$value);
                        fwrite($file,"\n");
                    }
                }
                fclose($file);
                return true;
            }else{
                return false;
            }
        }catch (\Exception $e){
            logWrite('fileContentAdd', $e->getMessage(), __FILE__ );
            throw new \Think\Exception(config("config.describe.error"));
        }
    }

    /**
     * 返回热词表的热词下标
     * @param $filePath // 文件路径
     * @param $words // 搜索值
     * @return array
     */
    public static function wordsIndex($filePath,$words){
        try{
            $fn = fopen($filePath,"r");
            $index = [];
            $i=0;
            while(!feof($fn))
            {
                $result = fgets($fn);
                # 去除换行符
                $result = str_replace(PHP_EOL, '', $result);
                $arrLine = explode("#", $result);
                if($arrLine[0] == $words){
                    array_push($index,$i);
                }
                $i++;
            }
            fclose($fn);
            // 有需要删除的热词
            if($index){
                return  $index;
            }else{
                return false;
            }
        }catch (\Exception $e){
            logWrite('updatedHotWords', $e->getMessage(), __FILE__ );
            throw new \Think\Exception(config("config.describe.error"));
        }
    }


    /**
     * 文件内容覆盖
     * @param $filePath // 文本文件路径
     * @param $array    // 覆盖的文件内容数组
     * @return bool
     */
    public static function fileContentCover($filePath,$array){
        try{
            $file = fopen($filePath,'w');
            $txt ='';
            if($array){
                foreach ($array as $key => $value){
                    if($value){
                        $txt.=$value."\n";
                    }
                }
                fwrite($file,$txt);
                fclose($file);
                return true;
            }else{
                return false;
            }
        }catch (\Exception $e){
            logWrite('fileContentCover', $e->getMessage(), __FILE__ );
            throw new \Think\Exception(config("config.describe.error"));
        }
    }

    /**
     * 文件内容清空
     * @param $filePath // 文本文件路径
     * @return bool
     */
    public static function emptyFile($filePath){
        try{
            $file = fopen($filePath,'w');
            fwrite($file,'');
            fclose($file);
            return true;
        }catch (\Exception $e){
            logWrite('emptyFile', $e->getMessage(), __FILE__ );
            throw new \Think\Exception(config("config.describe.error"));
        }
    }
}